<template>
  <div class="container">
    <div class="top">
      <div class="header">
        <a href="#/">
          <Logo class="logo"></Logo>
          <span class="title">学创校园</span>
        </a>
      </div>
      <div class="desc">
        学创校园 是谷阳世纪大厦最具影响力的 Web 组件范式
      </div>
    </div>
    <div class="main">
      <h3>注册</h3>
      <Form :model="form" ref="register" :rules="rules">
        <FormItem prop="mail">
          <Input v-model="form.mail" type="text" placeholder="邮箱" size="large"></Input>
        </FormItem>
        <FormItem prop="password">
          <Poptip placement="right" width="250" v-show="showPop" trigger="focus">
            <template slot="content">
              <div :class="passStatusMap[passStatus].className">{{ passStatusMap[passStatus].title}}</div>
              <Progress :percent="passPercent" hide-info :class="passStatusMap[passStatus].className" :stroke-width="6"></Progress>
              <div style="font-size:14px;line-height:1.5">
                请至少输入 6 个字符。请不要使用容易被猜到的密码。
              </div>
            </template>
            <Input v-model="form.password" type="password" placeholder="至少6位密码，区分大小写" size="large" ref="password"></Input>
          </Poptip>
          <Input v-model="form.password" type="password" placeholder="至少6位密码，区分大小写" size="large" v-show="!showPop"></Input>
        </FormItem>
        <FormItem prop="repassword">
          <Input v-model="form.repassword" type="password" placeholder="确认密码" size="large"></Input>
        </FormItem>
        <FormItem prop="phone">
          <Input v-model="form.phone" type="text" placeholder="11位手机号" size="large">
          <Select v-model="form.acode" slot="prepend" style="width: 60px">
            <Option value="+86">+86</Option>
            <Option value="+87">+87</Option>
          </Select>
          </Input>
        </FormItem>
        <FormItem prop="captcha">
          <Row type="flex" :gutter="8">
            <Col :span="16">
            <Input v-model="form.captcha" type="password" placeholder="验证码" size="large"></Input>
            </Col>
            <Col :span="8">
            <Button size="large" long @click="getCapcha" :disabled="disabled">
              <CountDown v-if="showCount" format="s" :target="target">
                <template slot="suffix">s</template>
              </CountDown>
              <template v-else>获取验证码</template>
            </Button>
            </Col>
          </Row>
        </FormItem>
        <FormItem>
          <Button type="primary" size="large" :loading="loading" class="submit" @click="handleSubmit">注 册</Button>
          <a href="#/user/login" class="login">使用已有账户登录</a>
        </FormItem>
      </Form>
    </div>
    <GlobalFooter :links="footLinks" class="footer">
      <div slot="copyright">Copyright &copy; 2017 <strong>学创校园</strong> 团队出品</div>
    </GlobalFooter>
  </div>
</template>
<script>
import { GlobalFooter, CountDown, Logo } from '@/components'
export default {
  components: { GlobalFooter, CountDown, Logo },
  data() {
    const validatePass = (rule, value, callback) => {
      if (value === '') {
        this.showPop = false
        callback(new Error('请输入密码！'))
      } else {
        this.showPop = true
        this.$nextTick(() => {
          this.$refs.password.focus()
        })
        if (value.length > 9) {
          this.passStatus = 'ok'
          this.passPercent = value.length * 10 > 100 ? 100 : value.length * 10
        } else if (value.length < 10 && value.length > 5) {
          this.passStatus = 'pass'
          this.passPercent = value.length * 10 > 100 ? 100 : value.length * 10
        } else {
          this.passStatus = 'pool'
          this.passPercent = value.length * 10 > 100 ? 100 : value.length * 10
        }
        // percent 可以抽出去,现在太重复了
        callback()
      }
    }
    const validatePassCheck = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请确认密码！'))
      } else if (value !== this.form.password) {
        callback(new Error('两次输入的密码不匹配!'))
      } else {
        callback()
      }
    }
    return {
      passStatus: 'pool',
      passPercent: 10,
      passStatusMap: {
        ok: {
          className: 'success',
          title: '强度：强',
          status: 'success'
        },
        pass: {
          className: 'warning',
          title: '强度：中',
          status: 'success'
        },
        pool: {
          className: 'error',
          title: '强度：太短',
          status: 'wrong'
        }
      },
      loading: false,
      disabled: false,
      showCount: false,
      target: 59000,
      showPop: false,
      form: {
        mail: '',
        password: '',
        repassword: '',
        acode: '+86',
        phone: '',
        captcha: ''
      },
      rules: {
        mail: [
          { required: true, message: '请输入邮箱地址！', trigger: 'change' },
          { type: 'email', message: '邮箱地址格式错误！', trigger: 'change' }
        ],
        password: [{ validator: validatePass, trigger: 'change' }],
        repassword: [{ validator: validatePassCheck, trigger: 'change' }],
        phone: [
          { required: true, message: '请输入手机号！', trigger: 'change' },
          { type: 'string', max: 11, message: '手机号格式错误！', trigger: 'change' },
          { type: 'string', min: 11, message: '手机号位数不足', trigger: 'blur' }
        ],
        captcha: [
          { required: true, message: '请输入验证码！', trigger: 'change' }
        ]
      },
      footLinks: [{
        title: '首页',
        href: '/'
      }, {
        title: '文档',
        href: '/'
      }, {
        title: 'GitLab',
        href: 'https://gitlab.oneitfarm.com/ladybird/ui-nuclear',
        blankTarget: true
      }]
    }
  },
  methods: {
    getCapcha() {
      this.disabled = true
      this.showCount = true
      this.target = 59000
      setTimeout(() => {
        this.disabled = false
        this.showCount = false
      }, 59000)
    },
    handleSubmit() {
      this.$refs.register.validate((valid) => {
        if (valid) {
          this.loading = true
          setTimeout(() => {
            this.$router.push({ name: 'user-result' })
          }, 1500)
        } else {
          return false
        }
      })
    }
  }
}

</script>
<style scoped>
.container {
  background: #f0f2f5;
  background-image: url(https://gw.alipayobjects.com/zos/rmsportal/TVYTbAXWheQpRcWDaDMu.svg);
  width: 100%;
  min-height: 100%;
  background-repeat: no-repeat;
  background-position: center;
  background-size: 100%;
  padding: 110px 0 144px 0;
  position: relative;
}

.top {
  text-align: center;
}

.header {
  height: 44px;
  line-height: 44px;
}

.header a {
  text-decoration: none;
}

.title {
  font-size: 33px;
  color: rgba(0, 0, 0, 0.85);
  font-family: 'Myriad Pro', 'Helvetica Neue', Arial, Helvetica, sans-serif;
  font-weight: 600;
  position: relative;
  top: 2px;
}

.desc {
  font-size: 14px;
  color: rgba(0, 0, 0, 0.45);
  margin-top: 12px;
  margin-bottom: 40px;
}

.footer {
  position: absolute;
  width: 100%;
  bottom: 0;
}

.main {
  width: 368px;
  margin: 0 auto;
}

.main h3 {
  font-size: 16px;
  margin-bottom: 20px;
}

.main .login {
  float: right;
  line-height: 40px;
  font-size: 14px;
}

.main .submit {
  width: 50%;
}

.success,
.warning,
.error {
  transition: color 0.3s;
  font-size: 14px;
}

.success {
  color: #52c41a;
}

.warning {
  color: #faad14;
}

.error {
  color: #f5222d;
}

</style>
<style>
.main .ivu-poptip,
.main .ivu-poptip-rel {
  width: 100%;
}

.main .ivu-poptip-inner {
  white-space: normal;
}

.main .error .ivu-progress-bg {
  background-color: #f5222d;
}

.main .warning .ivu-progress-bg {
  background-color: #faad14;
}

.main .success .ivu-progress-bg {
  background-color: #52c41a;
}

</style>
